﻿using Cxuu.Common;
using Cxuu.Models;
using Cxuu.Repository;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using System.Threading.Tasks;

namespace Cxuu.Service
{
    [UseDI(ServiceLifetime.Scoped)]
    public class LogService
    {
        private readonly LogRepository log;

        public LogService(LogRepository logRepository)
        {
            log = logRepository;
        }


        /// <summary>
        /// 获取全部列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public async Task<Page<ViewLogSql>> GetSqlListAsync(int page, int limit, int uid)
        {
            return await log.GetSqlListAsync(page, limit, uid, "Id desc");
        }

        public async Task<object> InsertLogSqlAsync(UserInfo userInfo, string dothing)
        {
            LogSql logSql = new()
            {
                UserId = userInfo.Id,
                DoThing = dothing,
                ControllerAction = userInfo.Controller + "/" + userInfo.Action,
                Ip = userInfo.Ip,
                Time = userInfo.NowTime
            };
            await log.AutoDelAsync(5000);//保留条数
            return await log.InsertAsync(logSql);
        }

    }
}
